package com.wskj.insuranceweb.reporter.controller;

import com.wskj.insuranceweb.reporter.pojo.*;
import com.wskj.insuranceweb.reporter.service.AcceptService;
import com.wskj.insuranceweb.reporter.service.ReporterService;
import com.wskj.insuranceweb.reporter.service.UserService;
import com.wskj.insuranceweb.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/reporter")
public class ReporterController {
    @Autowired
    private ReporterService service;

    @Autowired
    private UserService userService;
    @Autowired
    private AcceptService acceptService;




    @RequestMapping("/list1")
    public String list1(){
        return "reporter/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public PageInfo showReporter(String kname,String bname,Integer pageSize,Long offset){
        PageInfo page =  service.showList(kname,bname,pageSize,offset);
        return page;

    }

    @RequestMapping("/addReporter")
    public String addReporter(){
        return "reporter/addReporter";
    }

    @RequestMapping("/addReporter1")
    public String addReporter(Reporter reporter,HttpServletRequest request){

        Employee e = (Employee) request.getSession().getAttribute("employee");
        reporter.setEmployeename(e.getName());
        Date date =  new Date();
        SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
        String time = date1.format(date);
        reporter.setReportdate(time);

        reporter.setAccept("等待中");
       Users user =  userService.getUserById(reporter.getCustomernumber(),reporter.getAccidentdate());

       if(user != null){
           service.addReporter(reporter);
       }else{
           System.out.println("该客户已不在保险期间");
       }
        /*String date5 = reporter.getAccidentdate().replace("-","\\");
        reporter.setAccidentdate(date5);*/


        return "index";
    }

    @RequestMapping("/findReporter")
    public String findReporter(String id,Model model){
        Reporter re =  service.findReporter(id);
        model.addAttribute("reporter",re);
        return "reporter/look";
    }

    @RequestMapping("/addDocument")
    public String addDocument(String cust,Model model){
        model.addAttribute("cust",cust);
        return "reporter/addDocument";
    }
    @RequestMapping("/addDocument1")
    public String addDocument(Document document , MultipartFile name,HttpServletRequest request){

        String path ="C:\\Users\\Administrator\\IdeaProjects\\project\\insurance-web\\src\\main\\resources\\static\\image\\";
        String fileName = name.getOriginalFilename();
        File file = new File(path+fileName);
        try {
            name.transferTo(file);
        }catch (Exception e){
            e.printStackTrace();
        }
        document.setDocumentname(fileName);
        service.addDocument(document);

        return "reporter/update";
    }

    @RequestMapping("/updateAccept")
    public String updateAccept(String report){
        service.updateAccept(report);
        return " ";
    }
    @RequestMapping("/updatereporter")
    public String updateReporter(String id,String accept,String rejection){
        if(accept.equals("不予受理")){
            service.updateReporterNo(id,accept,rejection);
        }else{
            service.updateReporter(id,accept);
        }
        Reporter reporter = service.findReporter(id);
        if(reporter.getAccept().equals("受理中")){
            String a = reporter.getReportdate().substring(0,10);
            reporter.setReportdate(a);
            String i =reporter.getAccidentdate().substring(0,10);
            reporter.setAccidentdate(i);
            acceptService.addAccept(reporter);
            service.addBackuparea(reporter);
        }
        return "reporter/update";
    }
}
